import {Component, OnInit} from '@angular/core';
import {NzMessageService} from 'ng-zorro-antd/message';
import {Service} from '../service/service';
import {Router} from '@angular/router';
import {Config} from '../service/config';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  Config = Config;
  isOkLoading = false;
  passwordVisible = false;
  password: string;
  username: string;
  code: string;
  img: string;
  constructor(public msg: NzMessageService, public service: Service, private router: Router) {
  }

  ngOnInit(): void {
    this.getVerify();
  }

  getVerify(): void {
    this.img = Config.API_URL + 'admin/verify?mt=' + Math.random();
  }

   async login() {
    this.isOkLoading = true;
    const data = {
      username: this.username,
      password: this.password,
      code: this.code
    };
    const result: object = await this.service.login(data);
    console.log(result);
    this.isOkLoading = false;
    if (result.data.code === 200) {
      this.msg.info('登录成功');
      console.log(result.data.data);
      localStorage.setItem('manager', result.data.data);
      const {username, role_id, is_super} = result.data.data;
      localStorage.setItem('username', username);
      localStorage.setItem('role_id', role_id);
      localStorage.setItem('is_super', is_super);
      this.router.navigate(['layout']);
    } else {
      this.msg.error(result.data.msg);
    }
  }

}
